package com.cogent.business.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.cogent.business.domain.UserDomain;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.cache.EnumCacheManager;

public class HtmlUIHelper {
	public static final String HEAD_FIELD = "gistap";
	public static final String HEAD_VIEW = "gs";
	public static final String GLOBALS_SHOW_ERR_MESSAGE_INFORM = "show_err_message_inform";
	public static HashMap ATTRTYPEMAP=new HashMap();
	static{
		ATTRTYPEMAP.put("0", "文本");
		ATTRTYPEMAP.put("1", "数字");
		ATTRTYPEMAP.put("2", "字母");
		ATTRTYPEMAP.put("3", "单选项");
		ATTRTYPEMAP.put("4", "多选项");
		ATTRTYPEMAP.put("5", "日期"); 
	}
	
	public static String genDateInput(String name, String value) {
		return genDateInput(name, value, false, "");
	}
	public static String genDateInput(String name, String value, String notemsg) {
		return genDateInput(name, value, false, notemsg);
	}
	public static String genDateInput(String name, boolean isrequired) {
		return genDateInput(name, "", isrequired, "");
	}
	public static String genDateInput(String name, boolean isrequired, String notemsg) {
		return genDateInput(name, "", isrequired, notemsg);
	}
	public static String genDateInput(String name, String value, boolean isrequired, String notemsg) {
		StringBuffer str = new StringBuffer();
		str.append("<input type=\"text\" id=\"").append(name).append("\" name=\"").append(name)
			.append("\"  class=\"input_middclassle\" maxlength=\"10\"")
			.append(" value=\"").append(value).append("\" onkeypress=\"addYearMonthDay(this)\"/>");
		if(isrequired)
			str.append("&nbsp;<span class=\"txt_red\">*</span>");
		if(!StringUtil.isEmpty(notemsg))
			str.append("&nbsp;").append(notemsg);
		
		return str.toString();
	}
	
	public static String genDateInputCalendar(String name, String value, boolean isrequired, String notemsg) {
		return genDateInputCalendar(name, value, isrequired, notemsg, false);
	}
	
	public static String genDateInputCalendar(String name, String value, boolean isrequired, String notemsg, boolean isenable) {
		return genDateInputCalendar(name, value, isrequired, notemsg, isenable, "");
	}
	
	public static String genDateInputCalendar(String name, String value, boolean isrequired, String notemsg, boolean isenable, String js) {
		StringBuffer str = new StringBuffer();
		str.append("<input type=\"text\" id=\"").append(name).append("\" name=\"").append(name)
			.append("\"  class=\"input_middclassle\" readonly='readonly' maxlength=\"10\"")
			.append(" value=\"").append(value).append("\"");
		if(isenable)
			str.append(" onclick=\"setday(this,this,true)\"");
		if(!StringUtil.isEmpty(js))
			str.append(js);
		str.append("/>");
		if(isrequired)
			str.append("&nbsp;<span class=\"txt_red\">*</span>");
		if(!StringUtil.isEmpty(notemsg))
			str.append("&nbsp;").append(notemsg);
		
		return str.toString();
	}
	
	public static int getCurSize(int curpage, int pagesize, int num) {
		return (curpage-1) * pagesize + num;
	}
	
	public static String getRecycleReason(String code, String url) {
		StringBuffer str = new StringBuffer();
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String value = MapUtil.getStringValue(map, "value");
			if(value.equals("0"))
				continue;
			str.append("<li><a href=\"").append(url).append("&status=").append(value).append("\" class=\"adlink\">")
				.append(MapUtil.getStringValue(map, "enumitemname")).append("</a></li>");
		}
		
		return str.toString();
	}
	
	public static String getOmitSelectValue(String str) {
		String ret = str;
		if(str.equals("-100"))
			ret = "";
		
		return ret;
	}
	
	public static boolean isHead(Map formMap) {
		String value = MapUtil.getStringValue(formMap, HEAD_FIELD);
		boolean flag = true;
		if(value.equals(HEAD_VIEW))
			flag = false;
		
		return flag;
	}
	
	public static boolean isHead(HttpServletRequest request) {
		String value = "";
		Object obj = request.getParameter(HEAD_FIELD);
    	if(obj != null)
    		value = obj.toString();
		boolean flag = true;
		if(value.equals(HEAD_VIEW))
			flag = false;
		
		return flag;
	}
	
	public static boolean isButton(Map formMap) {
		String value = MapUtil.getStringValue(formMap, HEAD_FIELD);
		boolean flag = true;
		if(value.equals(HEAD_VIEW))
			flag = false;
		
		return flag;
	}
	
	public static boolean isButton(HttpServletRequest request) {
		String value = "";
		Object obj = request.getParameter(HEAD_FIELD);
    	if(obj != null)
    		value = obj.toString();
		boolean flag = true;
		if(value.equals(HEAD_VIEW))
			flag = false;
		
		return flag;
	}
	
	public static String genPopViewTap(Map formMap) {
		StringBuffer str = new StringBuffer();
		HashMap view_tap_map_url = new HashMap();
		view_tap_map_url.put("2", "PopFamily_view.action?isshow_content=1");
		view_tap_map_url.put("3", "Populate_listroom.action");
		view_tap_map_url.put("4", "Populate_listflow.action");
		view_tap_map_url.put("5", "DynamicForm_view.action?formid=POP_SOCIALREL");
		view_tap_map_url.put("2_CN", "家庭信息");
		view_tap_map_url.put("3_CN", "同屋成员");
		view_tap_map_url.put("4_CN", "流动记录");
		view_tap_map_url.put("5_CN", "社会关系");
		int type = MapUtil.getIntValue(formMap, "popregtype");
		String popurl = "Populate_view.action";
		if(type == 1)
			popurl = "RegPopulate_view.action";
		int curtap = MapUtil.getIntValue(formMap, "cur_tap");
		String curid = MapUtil.getStringValue(formMap, "cur_id");
		String curhouseid = MapUtil.getStringValue(formMap, "houseid");
		String curfamilyno = MapUtil.getStringValue(formMap, "familyno");
		String curcardno = MapUtil.getStringValue(formMap, "cur_cardno");
		String curreferer = MapUtil.getStringValue(formMap, "referer");
		if(StringUtil.isEmpty(curid)) {
			curid = MapUtil.getStringValue(formMap, "id");
			curhouseid = MapUtil.getStringValue(formMap, "stathouseid");
			curfamilyno = MapUtil.getStringValue(formMap, "familyno");
			curcardno = MapUtil.getStringValue(formMap, "cardno");
			str.append("<td id=\"menu_1\" class=\"info_nav_select\">人口信息</td>");
		}
		String prefix = "popregtype=" + type + "&cur_id=" + curid + "&houseid=" + curhouseid + "&familyno="
			+ curfamilyno + "&cur_cardno=" + curcardno + "&curcode=" + MapUtil.getStringValue(formMap, "curcode") 
			+ "&" + HEAD_FIELD + "=" + MapUtil.getStringValue(formMap, HEAD_FIELD) + "&referer=" + curreferer;
		
		if(!MapUtil.isEmpty(formMap, "cur_id"))
			str.append("<td id=\"menu_1\" class=\"info_nav_not_select\"><a href=\"").append(popurl + "?" + prefix + "&cur_tap=1&id=" + curid).append("\">人口信息</a></td>");
		String curclass = "info_nav_not_select";
		for(int i=2; i<=5; i++) {
			if(i == curtap)
				curclass = "info_nav_select";
			else
				curclass = "info_nav_not_select";
			String cururl = MapUtil.getStringValue(view_tap_map_url, "" + i);
			if(cururl.indexOf("?") > 0)
				cururl += "&";
			else
				cururl += "?";
			str.append("<td id=\"menu_1\" class=\"").append(curclass).append("\"><a href=\"").append(cururl + prefix + "&cur_tap=" + i)
				.append("\">").append(MapUtil.getStringValue(view_tap_map_url, "" + i + "_CN")).append("</a></td>");
		}
		str.append("<td class=\"info_nav_right\">&nbsp;</td>");
		
		return str.toString();
	}
	
	public static String getMutiSelect(String name, String typecode, String values) {
		StringBuffer str = new StringBuffer();
		List enmuList = (List)EnumCacheManager.get(typecode + "!LIST");
		Iterator it = enmuList.iterator();
		int num = 1;
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			str.append("<input type=\"checkbox\" name=\"").append(name)
				.append("\" id=\"").append(name)
				.append("\" value=\"").append(val).append("\"");
			if(values.indexOf("," + val + ",") >= 0)
				str.append(" checked");
			str.append("/>").append(MapUtil.getStringValue(map, "enumitemname")).append("&nbsp;");
				
		}
		
		return str.toString();
	}
	
	public static String getMutiSelectValue(String typecode, String values) {
		StringBuffer str = new StringBuffer();
		List enmuList = (List)EnumCacheManager.get(typecode + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(values.indexOf("," + val + ",") >= 0)
				str.append(MapUtil.getStringValue(map, "enumitemname")).append(",");
		}
		String ret = str.toString();
		if(ret.length()>0)
			ret = ret.substring(0, ret.length()-1);
		
		return ret;
	}
	
	public static String getCss(UserDomain userDomain) {
		StringBuffer str = new StringBuffer();
		str.append("<link href=\"css/index.css\" rel=\"stylesheet\" type=\"text/css\"/>");
		
		return str.toString();
	}
	
	public static String getOption(String option, String value, String curCode, int level) {
		String st = "";
		for(int i=1; i<level; i++)
			st += "--";
		StringBuffer str = new StringBuffer();
		str.append("<option value=\"").append(value).append("\"");
		if(value.equals(curCode))
			str.append(" selected=\"selected\"");
		str.append(">").append(st).append(option).append("</option>");
		
		return str.toString();
	}
	
	public static String getAttrtypeview(String value){  
		return MapUtil.getStringValue(ATTRTYPEMAP, value);
	}
	public static String getAttrtypeSelect(String name,String value,String classname,boolean isrequred){  
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(name).append("\" id=\"").append(name).append("\"")
			.append(classname).append("/>");
		Iterator it=ATTRTYPEMAP.keySet().iterator();
		for(int i=0;i<=5;i++){
			String val=""+i;
			String option=MapUtil.getStringValue(ATTRTYPEMAP, val);
			str.append(HtmlUIHelper.getOption(option, val, value, 0)); 
		} 
		
		str.append("</select>");
		if(isrequred)
			str.append("<span class=\"txt_red\">&nbsp;*</span>");
		return str.toString();
	}
	
	public static String getSFtypeview(String value){
		String str="<img src='images/";
		if("1".equals(value)){
			str+="list_true_icon.gif";
		}else{
			str+="list_false_icon.gif";
		}
		str+="' />";
		return str;
	}
	
}


